﻿using System.Collections.Generic;
using AutoMapper;
using Navigant.Application;
using Navigant.DataContracts;
using Navigant.Infrastructure;
using Navigant.Infrastructure.Cases;
using Navigant.Mapping;
using Navigant.Model.Custodians;
using Navigant.Model.PreservationNotices;
using Navigant.ServiceInterfaces;

namespace Navigant.WebServices
{
    public class PreservationNoticeService : IPreservationNoticeService
    {
        #region IPreservationNoticeService Members

        public List<PreservationNoticeTemplateContract> GetTemplates(CaseContract parentCase)
        {
            List<PreservationNoticeTemplateContract> contracts = new List<PreservationNoticeTemplateContract>();
            ICase currentCase = Converter.ToCase(parentCase);
            Navigant.Model.PreservationNotices.PreservationNoticeTemplateService service = new Navigant.Model.PreservationNotices.PreservationNoticeTemplateService(currentCase, null, true);
            IList<PreservationNoticeTemplate> templates = service.FindAll();
            foreach (PreservationNoticeTemplate template in templates)
            {
                contracts.Add(Mapper.Map<PreservationNoticeTemplate, PreservationNoticeTemplateContract>(template));
            }
            return contracts;
        }

        public int SaveTemplate(PreservationNoticeTemplateContract template)
        {
            PreservationNoticeTemplate mappedTemplate = Mapper.Map<PreservationNoticeTemplateContract, PreservationNoticeTemplate>(template);
            IUnitOfWork unitOfWork = new UnitOfWork();
            Navigant.Model.PreservationNotices.PreservationNoticeTemplateService service = new Navigant.Model.PreservationNotices.PreservationNoticeTemplateService(mappedTemplate.ParentCase, unitOfWork, true);
            service.SavePreservationNoticeTemplate(mappedTemplate);
            unitOfWork.Commit();
            return mappedTemplate.Id;
        }

        public List<CustodianContract> SendNotices(CaseContract parentCase, List<int> custodianIdentifiers,
            int preservationNoticeTemplateId, string fromAddress)
        {
            List<CustodianContract> mappedSendErrors = new List<CustodianContract>();
            ICase mappedCase = Converter.ToCase(parentCase);
            IList<Custodian> sendErrors = PreservationNoticeSendingService.SendNotices(mappedCase, 
                custodianIdentifiers, preservationNoticeTemplateId, fromAddress);
            foreach (Custodian custodian in sendErrors)
            {
                mappedSendErrors.Add(Mapper.Map<Custodian, CustodianContract>(custodian));
            }
            return mappedSendErrors;
        }

        public GetPreservationNoticesResultContract GetPreservationNotices(CaseContract parentCase, 
            int pageSize, int currentPage)
        {
            GetPreservationNoticesResultContract preservationNoticesResultContract = new GetPreservationNoticesResultContract();
            int itemCount;
            ICase currentCase = Converter.ToCase(parentCase);
            Navigant.Model.PreservationNotices.PreservationNoticeService service = new Navigant.Model.PreservationNotices.PreservationNoticeService(currentCase, null, true);
            IList<PreservationNotice> preservationNotices = service.GetPreservationNoticesBySite(pageSize, currentPage, out itemCount);
            preservationNoticesResultContract.TotalCount = itemCount;
            foreach (PreservationNotice preservationNotice in preservationNotices)
            {
                preservationNoticesResultContract.Result.Add(Mapper.Map<PreservationNotice, PreservationNoticeContract>(preservationNotice));
            }
            return preservationNoticesResultContract;
        }

        #endregion
    }
}
